package com.ygo.feihua;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ygo.feihua.BmobTable.GuangGao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.view.View.OnClickListener;
import android.support.v7.widget.Toolbar;
import android.support.v4.util.*;
import com.ygo.feihua.util.*;
import android.widget.*;

public class GengxinWangqi extends AppCompatActivity
{
	ListView wq_list;
	Gongju gj;
	DialogUtils du;
	List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gengxin_wangqi);
		wq_list=(ListView) findViewById(R.id.wq_list);
		gj=Gongju.getdx(this);
		du=DialogUtils.getdx(this);
		du.dialogj1(false,"","加载中,请稍等");
		gj.cshToolbar((Toolbar) findViewById(R.id.toolbar),"往期更新");

		
		
		BmobQuery<GuangGao> gg=new BmobQuery<GuangGao>();

		gg.addWhereEqualTo("gg_yc",2);

		gg.setLimit(50);

		gg.order("-createdAt");

		gg.findObjects(new FindListener<GuangGao>(){

				@Override
				public void done(List<GuangGao> p1, BmobException p2)
				{
					if(p2==null){
						for(GuangGao gg1:p1){
						Map<String,Object> ma=new ArrayMap<String,Object>();
						ma.put("name",gg1.getGg_name());
						ma.put("kb",gg1.getGg_tp());
						ma.put("wz",gg1.getGg_wz());
						ma.put("lx",gg1.getGg_lx());
							data.add(ma);
							
						}
						
						((BaseAdapter)wq_list.getAdapter()).notifyDataSetInvalidated();
						du.dis();
					}else{
						gj.toastBmobException(wq_list,"查询失败",p2);
						
					//	gj.show("查询失败"+p2.getMessage());
						du.dis();
					}
					// TODO: Implement this method
				}
			});
				
		
		
		
		wq_list.setAdapter(new BaseAdapter() {
                ViewHolder viewHolder = null;
				class ViewHolder{
					ImageView wq_kb;
					TextView wq_name;
				}

				@Override
				public View getView(final int position, View convertView, ViewGroup parent) {
					// TODO Auto-generated method
					if(convertView == null){
						viewHolder = new ViewHolder();
						convertView = getLayoutInflater().inflate(R.layout.item_wangqi, null);
						viewHolder.wq_kb=(ImageView) convertView.findViewById(R.id.wq_kb);
						viewHolder.wq_name=(TextView) convertView.findViewById(R.id.wq_name);
						convertView.setTag(viewHolder);
					}else{
						viewHolder=(ViewHolder)convertView.getTag();    
                    }
					viewHolder.wq_name.setText((String)data.get(position).get("name"));
					gj.tuxian(((BmobFile)data.get(position).get("kb")).getFileUrl(),viewHolder.wq_kb);
					viewHolder.wq_kb.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								String wz=(String)data.get(position).get("wz");

								switch ((int)data.get(position).get("lx"))
								{
									case 0:
										startActivity(IntentUtil.getUrlIntent(GengxinWangqi.this,wz));
										break;
									case 1:
										gj.tieziIntent(wz);
										break;
								}
								// TODO: Implement this method
							}
						});
					
					
					return convertView;
				}

				@Override
				public long getItemId(int position) {
					// TODO Auto-generated method stub
					return position;
				}

				@Override
				public Object getItem(int position) {
					// TODO Auto-generated method stub
					return data.get(position);
				}

				@Override
				public int getCount() {
					// TODO Auto-generated method stub
					return data.size();
				}
			});
		
		
	}
	
	
	
}
